// 引入Vue
import Vue from 'vue'
// 引入Vue-router
import Router from 'vue-router'

// 路由就是一个插件，需要use
Vue.use(Router)

// 引入layout组件
// layout组件非常重要
// 一级路由出口中，匹配layout组件
import Layout from '@/layout'

// 引入其他四个路由模块
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'

import bak from './bak.js'

// /**
//  * Note: sub-menu only appear when route children.length >= 1
//  * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
//  *
//  * hidden: true                   if set true, item will not show in the sidebar(default is false)
//  * alwaysShow: true               if set true, will always show the root menu
//  *                                if not set alwaysShow, when item has more than one children route,
//  *                                it will becomes nested mode, otherwise not show the root menu
//  * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
//  * name:'router-name'             the name is used by <keep-alive> (must set!!!)
//  * meta : {
//     roles: ['admin','editor']    control the page roles (you can set multiple roles)
//     title: 'title'               the name show in sidebar and breadcrumb (recommend set)
//     icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
//     noCache: true                if set true, the page will no be cached(default is false)
//     affix: true                  if set true, the tag will affix in the tags-view
//     breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
//     activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
//   }
//  */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// constantRoutes是静态路由
// 本项目，路由分成了两大块：静态路由 和 动态路由
// 静态路由：所有的用户都可以访问，不需要权限
// 动态路由：需要权限，如果有权限，就可以访问，如果没有权限，就不能访问
// 路由规则：就是一个对象
// path: '/redirect', 访问的url
// component: Layout, 访问出口中放什么组件  在一级路由出口中放 Layout 组件
// hidden: true,  隐藏  在侧边栏不能看到声明式导航
// children: [  配置二级路由  
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        // 在二级路由出口中放 @/views/redirect/index 组件
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    // 当访问 /login时，在一级路由出口中放登录页面  一级路由出口中是在APP组件中
    path: '/login',
    component: () => import('@/views/login/index'),
    // 在侧边栏不能看到声明式导航
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页大屏', icon: 'dashboard', affix: true }
      }
    ]
  },
  // {
  //   path: '/documentation',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/documentation/index'),
  //       name: 'Documentation',
  //       meta: { title: 'Documentation', icon: 'documentation', affix: true }
  //     }
  //   ]
  // },
  // {
  //   path: '/guide',
  //   component: Layout,
  //   redirect: '/guide/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/guide/index'),
  //       name: 'Guide',
  //       meta: { title: 'Guide', icon: 'guide', noCache: true }
  //     }
  //   ]
  // },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
// asyncRoutes是动态路由
// 页面级(路由级) 权限：
//    不同用户  登录到系统，看到的侧边栏时不一样的，也就是有不同的页面
//    同一个页面，有的用户可以访问，有的用户不能访问
// 并不是说，你在下面配置完就OK，背后还有很多代码
export const asyncRoutes = [
  {
    path: '/good',
    component: Layout,
    redirect: '/good/list',
    alwaysShow: true,
    name: 'GoodManage',
    meta: {
      title: '商品管理',
      icon: 'shopping',
      roles: ['admin', 'editor'],
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/good/good-list'),
        name: 'GoodList',
        meta: {
          title: '商品列表',
          roles: ['admin','editor'] 
        }
      },
      {
        path: 'add',
        component: () => import('@/views/good/good-form'),
        name: 'GoodForm',
        hidden: true,
        meta: {
          title: '添加商品',
          roles: ['admin','editor'] 
        }
      },
      {
        // path: 'edit/:id',  动态路由  在组件中可以获取id  $route.parms.id
        path: 'edit/:id',
        // 添加商品和编辑商品使用同一个组件
        component: () => import('@/views/good/good-form'),
        name: 'GoodForm',
        hidden: true,
        props: true, // 如果配置了这一行代码，就会把id传递给props
        meta: {
          title: '编辑商品',
          roles: ['admin','editor'] 
        }
      },
    ]
  },
  {
    path: '/student',
    component: Layout,
    redirect: '/student/list',
    alwaysShow: true,
    name: 'SutdentManage',
    meta: {
      title: '学生管理',
      icon: 'component',
      roles: ['admin', 'editor'],
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/student/student-list'),
        name: 'GoodList',
        meta: {
          title: '学生列表',
          roles: ['admin','editor'] 
        }
      },
      {
        path: 'add',
        component: () => import('@/views/student/student-form'),
        name: 'GoodForm',
        meta: {
          title: '添加学生',
          roles: ['admin','editor'] 
        }
      },
    ]
  },
  // 首页
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: 'Permission',
    meta: {
        // title: 'Permission',
        title: '首页',
        icon: 'lock',
        roles: ['admin', 'editor']
    },
    children: [
        {
            path: 'page',
            component: () => import('@/views/permission/page'),
            name: 'PagePermission',
            meta: {
            // title: 'Page Permission',
            title: '工作台',
            roles: ['admin']
            }
        },
        {
            path: 'directive',
            component: () => import('@/views/permission/directive'),
            name: 'DirectivePermission',
            meta: {
            // title: 'Directive Permission'
            title: '我的学期课务',
            }
        },
        {
            path: 'role',
            component: () => import('@/views/permission/role'),
            name: 'RolePermission',
            meta: {
            // title: 'Role Permission',
            title: '当前课务',
            roles: ['admin']
            }
        },
        {
            path: 'role',
            component: () => import('@/views/permission/role'),
            name: 'RolePermission',
            meta: {
            // title: 'Role Permission',
            title: '我的授课计划',
            roles: ['admin']
            }
        }
    ]
  },
  // 课务相关
  {
      path: '/icon',
      component: Layout,
      redirect: '/icon/page',
      alwaysShow: true, // will always show the root menu
      name: 'Permission',
      meta: {
          // title: 'Permission',
          title: '课务相关',
          icon: 'lock',
          roles: ['admin', 'editor']
      },
      children: [
          {
              path: 'index',
              component: () => import('@/views/icons/index'),
              name: 'Icons',
              meta: {
                // title: 'Icons',
                title: '课程安排',
                noCache: true
              }
          },
          {
              path: 'index',
              component: () => import('@/views/icons/index'),
              name: 'Icons',
              meta: {
                // title: 'Icons',
                title: '机房分配',
                noCache: true
              }
          }
      ]
  },
  // 综合管理
  {
      path: '/example',
      component: Layout,
      redirect: '/example/list',
      name: 'Example',
      meta: {
          // title: 'Example',
          title: '综合管理',
          icon: 'el-icon-s-help'
      },
      children: [
          {
              path: 'create',
              component: () => import('@/views/example/create'),
              name: 'CreateArticle',
              meta: {
                // title: 'Create Article',
                title: '用户管理',
                icon: 'edit'
              }
          },
          {
              path: 'edit/:id(\\d+)',
              component: () => import('@/views/example/edit'),
              name: 'EditArticle',
              meta: {
                // title: 'Edit Article',
                title: '班级管理',
                noCache: true,
                activeMenu: '/example/list'
              },
              hidden: true
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '专业管理',
                icon: 'list'
              }
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '课务管理',
                icon: 'list'
              }
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '课程管理',
                icon: 'list'
              }
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '学期管理',
                icon: 'list'
              }
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '时间段管理',
                icon: 'list'
              }
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '机房管理',
                icon: 'list'
              }
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '设备管理',
                icon: 'list'
              }
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '修改学生密码',
                icon: 'list'
              }
          },
          {
              path: 'list',
              component: () => import('@/views/example/list'),
              name: 'ArticleList',
              meta: {
                title: '导入数据',
                icon: 'list'
              }
          },
      ]
  },
  // 数据检索
  {
      path: '/tab',
      component: Layout,
      redirect: '/tab/page',
      alwaysShow: true,
      name: 'tab',
      meta: {
          title: '数据检索',
          icon: 'lock',
          roles: ['admin', 'editor']
      },
      children: [
          {
              path: 'index',
              component: () => import('@/views/tab/index'),
              name: 'Tab',
              meta: { title: '设备保障及维修', icon: 'tab' }
          },
          {
              path: 'index',
              component: () => import('@/views/tab/index'),
              name: 'Tab',
              meta: { title: '教学反馈', icon: 'tab' }
          },
          {
              path: 'index',
              component: () => import('@/views/tab/index'),
              name: 'Tab',
              meta: { title: '师生考勤记录', icon: 'tab' }
          },
          {
              path: 'index',
              component: () => import('@/views/tab/index'),
              name: 'Tab',
              meta: { title: '课务检索', icon: 'tab' }
          },
          {
              path: 'index',
              component: () => import('@/views/tab/index'),
              name: 'Tab',
              meta: { title: '机器使用检索', icon: 'tab' }
          },
          {
              path: 'index',
              component: () => import('@/views/tab/index'),
              name: 'Tab',
              meta: { title: '开门记录查询', icon: 'tab' }
          },
          {
              path: 'index',
              component: () => import('@/views/tab/index'),
              name: 'Tab',
              meta: { title: '违规使用', icon: 'tab' }
          },
      ]
  },
  // 反馈处理
  {
      path: '/error',
      component: Layout,
      redirect: 'noRedirect',
      name: 'ErrorPages',
      meta: {
          title: '反馈处理',
          icon: '404'
      },
      children: [
          {
              path: '401',
              component: () => import('@/views/error-page/401'),
              name: 'Page401',
              meta: { title: '设备报修处理', noCache: true }
          },
          {
              path: '404',
              component: () => import('@/views/error-page/404'),
              name: 'Page404',
              meta: { title: '违规处理', noCache: true }
          },
          {
              path: '404',
              component: () => import('@/views/error-page/404'),
              name: 'Page404',
              meta: { title: '违规标注', noCache: true }
          },
          {
              path: '404',
              component: () => import('@/views/error-page/404'),
              name: 'Page404',
              meta: { title: '违规组', noCache: true }
          },
      ]
  },
  // 设备命令注入
  {
      path: '/error-log',
      component: Layout,
      redirect: '/errorlog/page',
      alwaysShow: true,
      name: 'errorlog',
      meta: {
          title: '设备命令注入',
          icon: 'lock',
          roles: ['admin', 'editor']
      },
      children: [
          {
              path: 'log',
              component: () => import('@/views/error-log/index'),
              name: 'ErrorLog',
              meta: { title: '命令注入', icon: 'bug' }
          }
      ]
  },
  // 任务调度
  {
      path: '/excel',
      component: Layout,
      redirect: '/excel/export-excel',
      name: 'Excel',
      meta: {
          title: '任务调度',
          icon: 'excel'
      },
      children: [
          {
              path: 'export-excel',
              component: () => import('@/views/excel/export-excel'),
              name: 'ExportExcel',
              meta: { title: '任务管理' }
          },
          {
              path: 'export-selected-excel',
              component: () => import('@/views/excel/select-excel'),
              name: 'SelectExcel',
              meta: { title: '任务日志' }
          },
      ]
  },
  // 设置
  {
      path: '/zip',
      component: Layout,
      redirect: '/zip/download',
      alwaysShow: true,
      name: 'Zip',
      meta: { title: '设置', icon: 'zip' },
      children: [
          {
              path: 'download',
              component: () => import('@/views/zip/index'),
              name: 'ExportZip',
              meta: { title: '系统设置' }
          },
      ]
  },
  

  /** when your routing map is too long, you can split it into small modules **/
  // componentsRouter,
  // chartsRouter,
  // nestedRouter,
  // tableRouter,
  // ...bak,

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

// 创建一个路由对象   router
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
